<?php

namespace app\appapi\controller;


use app\appapi\service\UserTokenService;
use app\data\model\LiveAppList;
use app\data\model\LiveUserList;
use app\data\model\LiveUserLoginlog;

class Login extends BaseController
{
    //密码登录
    public function userPassLogin()
    {
        $post = $this->request->post();
        if(!isset( $post['user_name'], $post['user_password'], $post['app_info'])) $this->error('缺少参数！');
        if(!isset($post['app_info']['appName'])) $this->error('缺少appName参数！');
        if(!isset($post['app_info']['packageName'])) $this->error('缺少packageName参数！');
        if(!isset($post['app_info']['versionName'])) $this->error('缺少versionName参数！');
        if(!isset($post['app_info']['versionCode'])) $this->error('缺少versionName参数！');

        if($post['app_info']['versionCode'] < 7) $this->error('请在使用文档中更新到最新版本！低版本暂不支持登录！');

        //2.找到该用户ID
        $user = LiveUserList::mk()->where(['user_name' => $post['user_name'],'deleted' => 0])->find();
        if (empty($user)) $this->error('不存在该账户！');
        if ($user['user_password'] != $post['user_password']) $this->error('密码不正确！');
        if ($user['user_state'] == 1) $this->error('当前用户禁止登录！');
        //是否会员到期
        if ($user['thali_endtime'] < date('Y-m-d H:i:s')) $this->error('会员已到期，请联系客服！');
        //找到该应用，进行校验
        $app = LiveAppList::mk()->where(['app_id'=>$user['app_id']])->find();
        if($app['app_name'] != $post['app_info']['appName'] || $app['app_package'] != $post['app_info']['packageName']) $this->error('当前账户在该APP中不存在！');

        //生成token
        $tokenArr = UserTokenService::token($user['id']);
        if ($tokenArr[0] != 1) $this->error('token生成失败！');
        //更改用户
        LiveUserList::mk()->where(['user_name' => $post['user_name']])->update([
            'login_num' => $user['login_num'] + 1,
            'login_ip' => $this->request->ip(),
            'login_time' => date('Y-m-d H:i:s'),
            'login_appName' => $post['app_info']['appName'],
            'login_packageName' => $post['app_info']['packageName'],
            'login_versionName' => $post['app_info']['versionName'],
            'login_versionCode' => $post['app_info']['versionCode'],
        ]);

        $qa_status = $user['qa_status'];
        if($user['qa_status'] == 1 && $user['qa_endtime'] < date('Y-m-d H:i:s')){
            $qa_status = 0;
        }
        $train_status = $user['train_status'];
        if($user['train_status'] == 1 && $user['train_endtime'] < date('Y-m-d H:i:s')){
            $train_status = 0;
        }
        if (LiveUserLoginlog::mk()->where(['user_id' => $user['id']])->whereBetween('login_time', [date('Y-m-d') . ' 00:00:00', date('Y-m-d') . ' 23:59:59'])->count() == 0) {
            LiveUserLoginlog::mk()->insert(['user_id' => $user['id'], 'app_id' => $user['app_id'], 'login_time' => date('Y-m-d H:i:s')]);
        }
        $this->success('登录成功！', [
            'token'         => $tokenArr[2]['token'],
            'uuid'          => $tokenArr[2]['uuid'],
            'time'          => $tokenArr[2]['time'],
            'qa_status'     =>$qa_status,
            'train_status'  =>$train_status,
            'endtime'       =>$user['thali_endtime'],
            'nickname'       =>$user['user_nickname'],
        ]);
    }
}